package com.gene.utils;


import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 可以删除
 */
public class EHCache {

    public  void cache(HttpServletRequest request) throws  Exception{
        // 1. 创建缓存管理器

        String web_inf =request.getServletContext().getRealPath("/WEB-INF/");
        File file = new File(web_inf+"ehcache.xml");
        byte[] body = null;
        InputStream is = new FileInputStream(file);

        CacheManager cacheManager = CacheManager.create(is);

        // 2. 获取缓存对象
        Cache cache = cacheManager.getCache("standard");

        // 3. 创建元素
        Element element = new Element("key1", "value1");

        // 4. 将元素添加到缓存
        cache.put(element);

        // 5. 获取缓存
        Element value = cache.get("key1");
        System.out.println(value);
        System.out.println(value.getObjectValue());

        System.out.println(cache.getSize());

        // 6. 删除元素
        cache.remove("key1");
    }

    public static void main(String[] args)throws  Exception{
        EHCache cache=new EHCache();

    }
}
